﻿Imports System.Data
Imports VSModel
Imports Cls_Common

Partial Class Control_Quota_Edit
    Inherits System.Web.UI.UserControl
    Private Shared ReadOnly log As log4net.ILog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType)
#Region "Sub and Function "
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            If Not Request.QueryString("Id").ToString.Equals("0") Then
                hidID.Value = Request.QueryString("Id")
                LoadStoge()
                ShowData()
            End If
        End If
    End Sub
    Protected Sub LoadStoge()
        Using Data As New VSEntities
            Dim p = (From q In Data.Stores Order By q.Title Select q.StoreId, q.Title).ToList
            ddlStoge.AppendDataBoundItems = True
            ddlStoge.Items.Add(New ListItem("---Chọn---", 0))
            ddlStoge.DataTextField = "Title"
            ddlStoge.DataValueField = "StoreId"
            ddlStoge.DataSource = p
            ddlStoge.DataBind()
        End Using
    End Sub
    Protected Sub ShowData()
        Using data As New VSEntities
            Dim p As Quota = (From q In data.Quotas Where q.Id = hidID.Value Select q).SingleOrDefault
            If Not p Is Nothing Then
                ' txtQuotaNumber.Text = IIf(IsNothing(p.QuotaNumber) = True, "", p.QuotaNumber)
                ddlStoge.SelectedValue = IIf(IsNothing(p.StoreId) = True, 0, p.StoreId)
                txtProductCode.Text = IIf(IsNothing(p.ProductCode) = True, "", p.ProductCode)
                txtTotal.Text = IIf(IsNothing(p.Total) = True, "", p.Total)
            End If
        End Using
    End Sub
#End Region
#Region "Event for control"
    Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Using data As New VSEntities
            Dim p As Quota = (From q In data.Quotas Where q.Id = hidID.Value).SingleOrDefault
            Try
                p.Id = hidID.Value
                '  p.QuotaNumber = txtQuotaNumber.Text.Trim()
                p.StoreId = ddlStoge.SelectedValue
                p.Total = CInt(txtTotal.Text.Trim)
                p.ProductCode = txtProductCode.Text.Trim
                p.Modified = Now
                p.Modifier = Session("Username")
                Data.SaveChanges()
                'Insert_App_Log("Insert Advertise:" & txtTitle.Text.Trim & "", Function_Name.Advertisement, Audit_Type.Create, Request.ServerVariables("REMOTE_ADDR"), Session("UserName"))
                Excute_Javascript("AlertboxRedirect('Cập nhật dữ liệu thành công.','List.aspx');", Me.Page, True)
            Catch ex As Exception
                log4net.Config.XmlConfigurator.Configure()
                log.Error("Error error " & AddTabSpace(1) & Session("Username") & AddTabSpace(1) & "IP:" & GetIPAddress(), ex)
                Excute_Javascript("Alertbox('Cập nhật thất bại.');", Me.Page, True)
            End Try
        End Using
    End Sub
    Protected Sub btnHuy_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnHuy.Click
        Response.Redirect("List.aspx")
    End Sub
    Protected Sub hplSearch_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles hplSearch.Init
        hplSearch.NavigateUrl = "~/Page/BanHang/Popup.aspx?keepThis=true&TB_iframe=true&height=470&width=500&modal=true"

    End Sub
#End Region
End Class
